package app.server.project.main.service.db.entity.user;

import app.server.project.core.constants.Def;
import app.server.project.core.syndb.SynDBMgr;
import app.server.project.core.syndb.SynSpeed;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Transient;
import lombok.Data;
import org.hibernate.annotations.Comment;

import java.time.LocalDateTime;

@Data
@Entity(name = "user_wallet")
@TableName("user_wallet")
@Comment(value = "用户钱包信息表")
@SynSpeed
public class UserWalletEntity {

    @Id
    @Column(name = "id")
    @Comment("用户主键")
    @TableId("id")
    private long id;

    @Column(name = "user_id")
    @Comment("用户主键")
    @TableField("user_id")
    private long userId;


    @TableField("create_time")
    @Comment("创建时间")
    @Column(name = "create_time")
    private LocalDateTime createTime;

    @TableField("address_human")
    @Comment("钱包地址")
    @Column(name = "address_human")
    private String addressHuman;

    @TableField("block_type")
    @Comment("钱包类型")
    @Column(name = "block_type")
    private int blockType;

    @TableField("private_key")
    @Comment("钱包私钥")
    @Column(name = "private_key")
    private String privateKey;

    @TableField("address_hex")
    @Comment("16进制钱包地址")
    @Column(name = "address_hex")
    private String addressHex;

    @TableField("public_key")
    @Comment("钱包公钥")
    @Column(name = "public_key", length = 500)
    private String publicKey;

    @TableField("active")
    @Comment("激活状态")
    @Column(name = "active")
    private int active;


    @TableField("active_tx_id")
    @Comment("激活tron钱包交易id")
    @Column(name = "active_tx_id", length = 500)
    private String activeTxId;

    @TableField("active_tx_flag")
    @Comment("激活tron钱包交易id标记")
    @Column(name = "active_tx_flag")
    private int activeTxFlag;

    @TableField("last_transfer_time")
    @Comment("上次tron转账")
    @Column(name = "last_transfer_time")
    private LocalDateTime lastTransferTime;
    @Transient
    @TableField(exist = false)
    private boolean synDb = false;

    public static UserWalletEntity of(long userId, long id) {
        var ret = new UserWalletEntity();
        ret.setId(id);
        ret.setSynDb(true);
        ret.setUserId(userId);
        ret.setCreateTime(LocalDateTime.now());
        ret.setActive(Def.FALSE);
        return ret;
    }

    public static UserWalletEntity Null() {
        return new UserWalletEntity();
    }

    public void setActiveTxFlag(int activeTxFlag) {
        this.activeTxFlag = activeTxFlag;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserWalletEntity::getActiveTxFlag, this);
    }

    public void setActiveTxId(String activeTxId) {
        this.activeTxId = activeTxId;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserWalletEntity::getActiveTxId, this);
    }

    public void setLastTransferTime(LocalDateTime lastTransferTime) {
        this.lastTransferTime = lastTransferTime;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserWalletEntity::getLastTransferTime, this);
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserWalletEntity::getCreateTime, this);
    }

    public void setAddressHuman(String addressHuman) {
        this.addressHuman = addressHuman;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserWalletEntity::getAddressHuman, this);
    }

    public void setBlockType(int blockType) {
        this.blockType = blockType;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserWalletEntity::getBlockType, this);
    }

    public void setPrivateKey(String privateKey) {
        this.privateKey = privateKey;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserWalletEntity::getPrivateKey, this);
    }

    public void setAddressHex(String addressHex) {
        this.addressHex = addressHex;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserWalletEntity::getAddressHex, this);
    }


    public void setPublicKey(String publicKey) {
        this.publicKey = publicKey;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserWalletEntity::getPublicKey, this);
    }

    public void setUserId(long userId) {
        this.userId = userId;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserWalletEntity::getUserId, this);
    }

    public boolean isActive() {
        return active >= 1;
    }

    public void setActive(int active) {
        this.active = active;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserWalletEntity::getActive, this);
    }

    public boolean isTx() {
        return this.activeTxFlag >= 1;
    }
}
